package com.inno.subject.controller.controller;

import com.google.common.base.Preconditions;
import com.inno.subject.common.annotiation.Log;
import com.inno.subject.common.context.LoginUserContext;
import com.inno.subject.common.entity.CommonReturnType;
import com.inno.subject.common.entity.PageResult;
import com.inno.subject.controller.convert.SubjectCollectDTOConverter;
import com.inno.subject.controller.dto.SubjectCollectDTO;
import com.inno.subject.domain.entity.SubjectCollectBO;
import com.inno.subject.domain.service.SubjectCollectDomainService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author sean
 * 题目收藏controller
 */
@RestController
@RequestMapping("/subject/collect")
public class SubjectCollectController {
    @Resource
    private SubjectCollectDomainService subjectCollectDomainService;

    /**
     * 收藏
     */
    @PostMapping("/add")
    @Log(operationType = "收藏题目")
    public CommonReturnType<Void> add(@RequestBody SubjectCollectDTO subjectCollectDTO) {
        Preconditions.checkNotNull(subjectCollectDTO.getSubjectId(),"题目id不能为空");
        subjectCollectDTO.setCurrentUser(LoginUserContext.getUserId());
        SubjectCollectBO subjectInfoBO = SubjectCollectDTOConverter.INSTANCE.convertDTOToBO(subjectCollectDTO);

        subjectCollectDomainService.add(subjectInfoBO);
        return CommonReturnType.success();
    }

    @PostMapping("/queryPage")
    public CommonReturnType<PageResult<SubjectCollectDTO>> queryPage(@RequestBody SubjectCollectDTO subjectCollectDTO){
        Preconditions.checkNotNull(subjectCollectDTO.getPage(),"分页参数不能为空");
        Preconditions.checkNotNull(subjectCollectDTO.getPageSize(),"分页参数不能为空");
        subjectCollectDTO.setCurrentUser(LoginUserContext.getUserId());
        SubjectCollectBO subjectCollectBO = SubjectCollectDTOConverter.INSTANCE.convertDTOToBO(subjectCollectDTO);
        PageResult<SubjectCollectBO> boResult = this.subjectCollectDomainService.queryPage(subjectCollectBO);
        List<SubjectCollectDTO> dtoList = SubjectCollectDTOConverter.INSTANCE.convertBOListToDTOList(boResult.getList());
        PageResult<SubjectCollectDTO> result = PageResult.of(dtoList, boResult.getTotal());
        return CommonReturnType.success(result);
    }
}
